<template>
  <AssetHolderSelectList
      ref="refHoldList"
      :tag="'用户'"
      :name='props.entry.name'
      :columns="columns"
      :can-join="canJoin"
      :readonly="props.readonly"
      :load-holders="loadAssetHolders"
      :join-holders="joinAssetHolders"
      :load-join-holders="loadJoinAssetHolders"
      :comment="props.comment"
  />
</template>

<script setup lang="ts">
import {computed, onMounted, ref} from 'vue'
import {useLoadAssign} from '@/utils/sop/UseLoadAssign'
import AssetHolderSelectList from '@/views/sop/holder/AssetHolderSelectList.vue'
import {getStateOptions, getStateTag, getStateText} from "@/utils/SystemUtil";
import {currentNames} from "@/stores/conf";
import {hasPermit} from "@/utils/sop/PermitUtil";

const refHoldList = ref();

const props = withDefaults(
    defineProps<{
      entry: any,
      holdType: string,
      comment?: string,
      readonly ?: boolean,
      loadHolders?: any,
      joinHolders?: any,
      loadJoinHolders?: any,
      removeFromHolder?: any,
    }>(),
    {}
)

const { loadHolders, joinHolders, loadJoinHolders, removeAssetFromHolder} = useLoadAssign(props.entry)

const canJoin: any = computed(() => {
  if (currentNames.PERMIT == props.entry.name) {
    return hasPermit('asset:permit:join-user');
  } else if (currentNames.MENU == props.entry.name) {
    return hasPermit('asset:menu:join-user');
  } else if (currentNames.ROLE == props.entry.name) {
    return hasPermit('asset:role:join-user');
  }
})

const canRemove = () => {
  if (currentNames.PERMIT == props.entry.name) {
    return hasPermit('asset:permit:remove-user');
  } else if (currentNames.MENU == props.entry.name) {
    return hasPermit('asset:menu:remove-user');
  } else if (currentNames.ROLE == props.entry.name) {
    return hasPermit('asset:role:remove-user');
  }
}

const getTargetId = () => {
  return '';
}

const columns = ref([
  {
    id: 'id',
    name: '标识',
    hideCol: true,
    show: false,
  },
  {
    id: 'name',
    name: '用户名称',
    show: true,
  },
  {
    id: 'phone',
    name: '电话',
    show: true,
  },
  {
    id: 'state',
    name: '状态',
    show: true,
    tag: 'true',
    type: 'select',
    tagType: (tag: number) => { return getStateTag(tag); },
    tagText: (tag: number) => { return getStateText(tag); },
    options: getStateOptions()
  },
  {
    id: 'lastLoginTime',
    name: '最后一次登录',
    show: true,
  },
  {
    id: 'createTime',
    name: '创建时间',
    show: true,
  },
  {
    id: 'operation',
    name: '操作',
    show: true,
    canEdit: false,
    hideCol: true,
    fixed: 'right',
    operations: [
      {
        type: 'confirm',
        name: '移除',
        disabled: (row: any) => {
          return row.noAuth == 1 || !canRemove();
        },
        tips: () => { return '确认移除？' },
        confirm: async (data: any, row: any) => {
          if (props.removeFromHolder) {
            await props.removeFromHolder(row);
          } else {
            await removeAssetFromHolder(getTargetId(), props.holdType, props.entry, row);
          }
          refresh();
        }
      }
    ]
  }
]);

const loadAssetHolders = async (params: any) => {
  if (props.loadHolders) {
    return await props.loadHolders(params);
  } else {
    return await loadHolders(getTargetId(), props.holdType, params || {})
  }
}

const joinAssetHolders = async (items: any) => {
  if (props.joinHolders) {
    return await props.joinHolders(items);
  } else {
    return await joinHolders(getTargetId(), props.holdType, items)
  }
}

const loadJoinAssetHolders = async (params: any) => {
  let result: any;

  if (props.loadJoinHolders) {
    result = await props.loadJoinHolders(params);
  } else {
    result = await loadJoinHolders(getTargetId(), props.holdType, params || {})
  }

  return result;
}

onMounted(() => {
  if (props.readonly) {
    columns.value.pop();
  }
})

const refresh = () => {
  refHoldList.value?.refresh();
}

defineExpose({
  refresh,
})

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}

</style>
